//
//  ViewController.m
//  DrawRect
//
//  Created by cocoazxj@163.com on 15/4/16.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "ViewController.h"
#import "TipViewController.h"
#import "CustomView.h"

@interface ViewController ()
@property (nonatomic, strong) IBOutlet UIImageView *iv_draw;
@property (nonatomic, strong) IBOutlet CustomView *customView;
@property (nonatomic, strong) IBOutlet UISlider *slider;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.customView.radius = self.slider.value;
    [self shotScreen];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)modalButtonClicked:(id)sender
{
//    [self presentViewController: animated:<#(BOOL)#> completion:<#^(void)completion#>]
}

- (IBAction)sliderCircle:(UISlider *)sender
{
    self.customView.radius = sender.value;
    [self.customView setNeedsDisplay];
}

// 截屏功能
- (void)shotScreen
{
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
//    UIImage *image = [[UIImage alloc] init];
//    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0);
//    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];
    // 获取生成的图片
    UIImage *new = UIGraphicsGetImageFromCurrentImageContext();
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"shot.png"];
    NSLog(@"------------%@",path);
    // 把UIImage数据类型转化为NSData类型
    NSData *data = UIImagePNGRepresentation(new);
    [data writeToFile:path atomically:YES];
    
    UIImageWriteToSavedPhotosAlbum(new, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if(error)
    {
        NSLog(@"失败！");
    }
    else
    {
        NSLog(@"成功！");
    }
}

// 添加水印
- (void)addWaterMark
{
    UIImage *image = [UIImage imageNamed:@"xiao"];
    // 如果指定大小，图片显示不全；
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    [image drawAtPoint:CGPointMake(0, 0)];
    NSString *string = @"Hello!";
    [string drawInRect:CGRectMake(30, 70, 40, 30) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20], NSForegroundColorAttributeName:[UIColor redColor]}];
    UIImage *new = UIGraphicsGetImageFromCurrentImageContext();
    self.iv_draw.image = new;
}

// 绘制图片图层，把图片图层保存起来。
- (void)saveImageLayer
{
    // 创建Bitmap
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(context, CGRectMake(10, 10, 40, 40));
    CGContextStrokePath(context);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    self.iv_draw.image = image;
    
    // 存储路径
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"circle.png"];
    NSLog(@"------------%@",path);
    // 把UIImage数据类型转化为NSData类型
    NSData *data = UIImagePNGRepresentation(image);
    [data writeToFile:path atomically:YES];
}
@end
